<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class teacher_model extends CI_Model {

    public function __construct() {
        error_reporting(0);
        parent::__construct();
    }
    
    public  function search_teacher($keyword , $major="*"){
          $this->db->select("SQL_CALC_FOUND_ROWS  *",false)->from("teachers")
                         ->join("majors","majors.major_id =  teachers.ref_major_id","left")
                         ->where("( concat( teacher_name , ' ' , teacher_code  )   LIKE   '%$keyword%'  )",null,false)
                         ->order_by("teacher_id","ASC");
          if($major != "*"){
              $this->db->where("majors.major_id  =",$major);
          }
          $this->db->limit(15,$_POST["page-limit"]);
          return $this->db->get();
    }


    public function get_teacher_by_id($teacher_id){
        return $this->db->select(" * ")->from("teachers")->join("majors","majors.major_id =  teachers.ref_major_id","left")->where("teachers.teacher_id",$teacher_id)->limit(1)->get()->row();
    }
    
    public function get_teachers_in_activity( $act_id ){
        $this->db->select("*")->from("activity_events")
                ->join("activity_groups_relation"," activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                ->join("group_teacher_relation","group_teacher_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id")
                ->join("teachers","teachers.teacher_id = group_teacher_relation.ref_teacher_id","inner")
               ->group_by("group_teacher_relation.ref_teacher_id");
       return $this->db->get();
    }
    
   public function getTeacher(){
       $start = ($_POST["page-limit"]=="")?0:$_POST["page-limit"];
       $keyword = $_POST["txt_search"];
       return $this->db->select(" SQL_CALC_FOUND_ROWS  * ",false)
               ->from("teachers")
               ->join("majors","majors.major_id =  teachers.ref_major_id","left")
               ->order_by("teacher_name","ASC")
               ->where("( concat( teacher_name , ' ' , teacher_code  )   LIKE   '%$keyword%'  )",null,false)
               ->limit(15,$start)->get();
   }
   
  public function get_teacher_activitys($teacher_id){
        $this->db->select("*")->from("activity_events")
                ->join("activity_groups_relation"," activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                ->join("group_teacher_relation","group_teacher_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id")
                ->join("teachers","teachers.teacher_id = group_teacher_relation.ref_teacher_id","inner")
                ->where("teachers.teacher_id",  "$teacher_id");
       return $this->db->get();
  }
   
   public function get_report($tid){
                    $this->db->select("*")->from("activity_events")
                            ->join("activity_groups_relation","activity_groups_relation.ref_act_id = activity_events.act_id","inner")
                            ->join("group_teacher_relation","group_teacher_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id")
                            ->join("group_major_relation","group_major_relation.ref_activity_groups_relation_id = activity_groups_relation.activity_groups_relation_id")
                            ->join("collegians_of_activity" , "collegians_of_activity.ref_group_major_relation_id = group_major_relation.group_major_relation_id")
                            ->where("group_teacher_relation.ref_teacher_id",$tid)
                            ->where("act_type","YES")
                            ->where("status","OPEN")
                            ->group_by("activity_events.act_id")
                            ->order_by("activity_events.act_start_date","DESC");
                     return $this->db->get();
   }
   
   public function update($post){
       $date = $post["year"] . "-" . $post["month"] . "-" . $post["day"];
       $sql = "UPDATE  teachers  SET 
                                  teacher_code='$post[txt_code]' ,  
                                  teacher_date='$date' ,   
                                  teacher_name='$post[txt_name]'  ,
                                  gender='$post[gender]' ,
                                  teacher_phone='$post[phone_number]' ,
                                  ref_major_id='$post[group_type]' 
                   WHERE  teacher_id='$post[teacher_id]'  ";
       $this->db->query($sql);
   }
   
}
?>
